package com.itheima.udp2;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;

public class UDPClient1 {

    public static void main(String[] args) throws Exception {

        //简单的UDP通信多发多收：客户端开发

        System.out.println("==========客户端启动==========");

        //1.创建发送端对象
        DatagramSocket socket = new DatagramSocket();       //默认随机分配一个端口号

        Scanner sc = new Scanner(System.in);
        while (true) {
            //2.创建数据包对象封装要发送的数据
            System.out.print("请输入要发送的数据：");
            String msg = sc.nextLine();     //读取整行，不受空格影响
            if ("exit".equals(msg)) {
                System.out.println("==========客户端退出==========");
                socket.close();
                break;
            }
            byte[] bytes = msg.getBytes();
            DatagramPacket packet = new DatagramPacket(bytes, bytes.length, InetAddress.getLocalHost(), 8080);

            //3.让发送端对象发送数据包的数据
            socket.send(packet);
        }
    }
}
